<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Model_Post extends Zend_Db_Table{
	
	protected $_name    = "posts";
    protected $_primary = "id";
    
    public function getDatas($options = null, $cols = array(), $join = false){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        if(count($cols) == 0){
            $cols = array('id', 'title', 'alias', 'description', 'image', 'content', 'created', 'created_by');
        }
        
        $select = $db->select()
                    ->from(array('p' => 'posts'), $cols)
                    ->where('p.status = 1');  
        
        if($join){
            $select->join(array('pc' => 'post_categories'), "p.category = pc.id", array('category'));
        }
        
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        if(($options != null && !array_key_exists('order', $options)) || $options == null){
            $select->order('p.created DESC');
        }
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getQuery($options = null, $cols = array(), $join = true){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        if(count($cols) == 0){
            $cols = array('id', 'title', 'alias', 'description', 'category', 'image', 'content', 'created', 'comment');
        }
        
        $select = $db->select()
                    ->from(array('p' => 'posts'), $cols)
                    ->where('p.status = 1');
        
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        if(($options != null && !array_key_exists('order', $options)) || $options == null){
            $select->order('p.created DESC');
        }
        
        return $select;
    }
    
    public function getData($id, $cols = array(), $join = false){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            if(count($cols) == 0){
                $cols = array('id', 'title', 'alias', 'description', 'image', 'content', 'created', 'created_by');
            }
            
            $select = $db->select()
                        ->from(array('p' => 'posts'), $cols)
                        ->where('p.id = ?', $id, 'INTEGER')
                        ->where('p.status = 1');
            
            if($join){
                $select->join(array('pc' => 'post_categories'), "p.category = pc.id", array('category'));
            }
            
            $rs = $db->fetchRow($select);
            
            return $rs;
        }
        
        return false;
    }
    
    public function checkExists($id){
        $option = array('table' => 'posts', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
}